package com.yanqu.road.dao.impl.activity.langyamemorial;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.langyamemorial.CrossLangYaMemorialBuildData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CrossLangYaMemorialBuildDataDaoImpl extends TempDataObjectDao<CrossLangYaMemorialBuildData> {


    public static String getCrossLangYaMemorialBuildDataSql = "select * from `t_c_activity_langya_memorial_build_data` where `activityId`=? order by buildNo desc limit 1000";
    public List<CrossLangYaMemorialBuildData> getCrossLangYaMemorialBuildData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<CrossLangYaMemorialBuildData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossLangYaMemorialBuildDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossLangYaMemorialBuildData data = parse(rs);
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateCrossLangYaMemorialBuildDataSql = "update `t_c_activity_langya_memorial_build_data` set buildType=?,userId=? where activityId=? and buildNo=?";
    @Override
    protected boolean updateData(CrossLangYaMemorialBuildData crossLangYaMemorialBuildData) {
        crossLangYaMemorialBuildData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossLangYaMemorialBuildData.getBuildType()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLangYaMemorialBuildData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossLangYaMemorialBuildData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLangYaMemorialBuildData.getBuildNo()));
        if (getDb().executeNoneQuery(updateCrossLangYaMemorialBuildDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossLangYaMemorialBuildDataSql = "insert into `t_c_activity_langya_memorial_build_data`(activityId,buildNo,buildType,userId) values (?,?,?,?)";
    @Override
    protected boolean addData(CrossLangYaMemorialBuildData crossLangYaMemorialBuildData) {
        crossLangYaMemorialBuildData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossLangYaMemorialBuildData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLangYaMemorialBuildData.getBuildNo()));
        para.put(i++, new DbParameter(Types.INTEGER, crossLangYaMemorialBuildData.getBuildType()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLangYaMemorialBuildData.getUserId()));
        if (getDb().executeNoneQuery(insertCrossLangYaMemorialBuildDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossLangYaMemorialBuildData parseData(ResultSet rs) throws SQLException {
        CrossLangYaMemorialBuildData data = new CrossLangYaMemorialBuildData();
        data.setActivityId(rs.getInt("activityId"));
        data.setBuildNo(rs.getLong("buildNo"));
        data.setBuildType(rs.getInt("buildType"));
        data.setUserId(rs.getLong("userId"));

        return data;
    }
}
